package com.eyas.business.controllor.admininfo;

import com.eyas.business.model.jdbcEntity.table.AdminInfo;
import com.eyas.business.model.jdbcEntity.table.CoachInfo;
import com.eyas.business.model.jdbcEntity.table.RoleInfo;
import com.eyas.business.model.pojo.admininf.QueryPageParmDTO;
import com.eyas.business.model.pojo.admininf.QueryPageResultDTO;
import com.eyas.business.service.admininfo.AdminInfoService;
import com.eyas.business.service.role.RoleService;
import com.eyas.core.model.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/2 15:29
 * @Description:
 */
@Controller
@RequestMapping("/eyas/admininfo/")
public class AdminInfoAction {

    @Autowired
    private AdminInfoService adminInfoService;
    @Autowired
    private RoleService roleService;

    @GetMapping("/queryAdminInfo")
    @ResponseBody
    public PageResult<QueryPageResultDTO> queryAdminInfo(QueryPageParmDTO queryPageParmDTO,
                                                         @RequestParam(defaultValue = "1") int page,
                                                         @RequestParam(defaultValue = "10") int limit) {
        PageResult<QueryPageResultDTO> pageResult = adminInfoService.queryAdminInfo(queryPageParmDTO,page-1,limit);
        return pageResult;
    }

    @PostMapping("/changeValidflag")
    @ResponseBody
    public String changeValidflag(int adminnid) {
        this.adminInfoService.updateValidFlag(adminnid);
        return "{\"success\":true}";
    }

    @RequestMapping("/edit")
    public String edit(Integer adminnid, Model model) {
        if(adminnid!=null) {
            QueryPageResultDTO adminInfo = adminInfoService.getAdminInfo(adminnid);
            model.addAttribute("admininfo",adminInfo);
        }
        List<RoleInfo> roleInfos = roleService.queryAll();
        model.addAttribute("roleinfos",roleInfos);
        return "/admininfo/edit";
    }

    @PostMapping("/saveAdminInfo")
    @ResponseBody
    public String saveAdminInfo(QueryPageParmDTO adminInfo, HttpSession session) {
        this.adminInfoService.saveAdminInfo(adminInfo,session);
        return "{\"success\":true}";
    }
    /**
     * 获取admininfo select列表
     * @return
     */
    @GetMapping("/queryAdminInfoList")
    @ResponseBody
    public List<AdminInfo> queryAdminInfoList() {
        return  adminInfoService.queryAdminInfoList();
    }


    @RequestMapping("/passwordedit")
    public String passwordedit(Integer adminnid, Model model) {
        QueryPageResultDTO adminInfo = adminInfoService.getAdminInfo(adminnid);
        model.addAttribute("admininfo",adminInfo);
        return "/admininfo/passwordedit";
    }


    @PostMapping("/modifyPassword")
    @ResponseBody
    public String modifyPassword(QueryPageParmDTO pageParmDTO) {
        this.adminInfoService.modifyPassword(pageParmDTO);
        return "{\"success\":true}";
    }
}
